package com.luxury.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.google.gson.Gson;
import com.luxury.R;
import com.luxury.bean.BannerContentBean;
import com.luxury.bean.HomeBean;
import com.luxury.utils.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by Administrator on 2016/12/10.
 */

public class HomeRecyclerAdapter1 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext = null;
    private List<HomeBean.FloorsBean> mList = null;
    private RecyclerView mRv = null;
    private LayoutInflater mInflater = null;
    public static final String TAG = "HomeRecycerAdapter";

    public HomeRecyclerAdapter1(Context _context, List<HomeBean.FloorsBean> _list) {
        this.mContext = _context;
        this.mList = _list;
//        this.mRv=_rv;
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    //item多布局
    @Override
    public int getItemViewType(int position) {
        Log.i("tag", "onCreateViewHolder-------------------------->>>>getType: "+mList.get(position).getType());
        return mList.get(position).getType();
    }

    //   创建viewholder
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case 1:
                Log.i("tag", "onCreateViewHolder-------------------------->>>>: "+viewType);

                view = mInflater.inflate(R.layout.item_type1_homefragment, parent, false);
                return new ViewHolderBanner(view);
            case 2:
                Log.i("tag", "onCreateViewHolder-------------------------->>>>: "+viewType);

                view = mInflater.inflate(R.layout.item_banner_homefragment, parent, false);
                return new ViewHolderBanner(view);
            case 3:
                Log.i("tag", "onCreateViewHolder: ----------------->>>>>>>>>>>.."+viewType);
                view = mInflater.inflate(R.layout.item_type3_homefragment, parent, false);
                return new ViewHolderBanner(view);

        }
        return null;
    }

    //绑定viewholder
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Log.i(TAG, "onBindViewHolder:--->> " + (holder instanceof ViewHolderBanner));
//      如果是banner类型的
        if (holder instanceof ViewHolderBanner) {
//            设置banner
            initBanner(holder, position);
        }
//        else if(holder instanceof ViewHolderType3){
//            String json = mList.get(position).getContent();
//            Log.i(TAG, "Type1的content-->>"+json);
////            gosn解析
//            Gson gson = new Gson();
////            gson.fromJson(json)
//        }
        else if(holder instanceof ViewHolderType1){
            String json = mList.get(position).getContent();
            Log.i(TAG, "Type1的content-->>"+json);
//            gosn解析
            Gson gson = new Gson();
//            gson.fromJson(json)
        }

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    //--------------------------banner的holder---------------------------------------------
    class ViewHolderBanner extends RecyclerView.ViewHolder {
        @BindView(R.id.banner_item_homefragment)
        Banner banner_ItemHomefragment;

        public ViewHolderBanner(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    //--------------------------Type1的holder---------------------------------------------
    class ViewHolderType1 extends RecyclerView.ViewHolder {
        @BindView(R.id.layout_item_type1)
        RadioGroup layout_item_type1;
        public ViewHolderType1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    //--------------------------Type3的holder---------------------------------------------
    class ViewHolderType3 extends RecyclerView.ViewHolder {
        @BindView(R.id.view_item_type3)
        RadioGroup view_item_type3;
        public ViewHolderType3(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    //---------------------------封装设置banner的方法------------------------------------------
    private void initBanner(RecyclerView.ViewHolder holder, int position) {
        Log.i(TAG, "轮播图的content-->>" + mList.get(position).getContent());
//            用来存储图片的list
        List<String> list_bannerImg = new ArrayList<>();
//            得到图片数据

        String json = mList.get(position).getContent();
//            gosn解析
        Gson gson = new Gson();
        List<BannerContentBean.BannerBean> list_banner = gson.fromJson(json, BannerContentBean
                .class).getBanner();
        for (int i = 0; i < list_banner.size(); i++) {
//                String img = list_banner.get(i).getImg();
//                将图片放到图片集合中
            list_bannerImg.add(list_banner.get(i).getImg());
        }
        //给banner设置样式
        ((ViewHolderBanner) holder).banner_ItemHomefragment.setBannerStyle(BannerConfig
                .CIRCLE_INDICATOR);
        //设置图片加载器
        ((ViewHolderBanner) holder).banner_ItemHomefragment.setImageLoader(new GlideImageLoader());
        //设置图片集合
        ((ViewHolderBanner) holder).banner_ItemHomefragment.setImages(list_bannerImg);
        //设置banner动画效果
        ((ViewHolderBanner) holder).banner_ItemHomefragment.setBannerAnimation(Transformer
                .DepthPage);
        //设置自动轮播，默认为true
        ((ViewHolderBanner) holder).banner_ItemHomefragment.isAutoPlay(true);
        //设置轮播时间
        ((ViewHolderBanner) holder).banner_ItemHomefragment.setDelayTime(1500);
        //设置指示器位置（当banner模式中有指示器时）
        ((ViewHolderBanner) holder).banner_ItemHomefragment.setIndicatorGravity(BannerConfig
                .CENTER);
        //banner设置方法全部调用完毕时最后调用
        ((ViewHolderBanner) holder).banner_ItemHomefragment.start();
    }


}
